﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using FBEntities.FBObjects.User;
using XFItemControls.XFPanels.XFPanelItems;
using FBEntities.FBConnections.User;

namespace XFControls.XFPanels.XFPanelItems
{
    enum ProfileTabs
    {
        Info,
        Wall,
        Photos,
        None
    }
    public class XFItemProfileTabs : IXFItemReceiveClick
    {
        public FBPerson User { get; set; }

        private int _tabHeigt = 45;
        private int _textPadding = 15;
        private ProfileTabs curTab;
        private ProfileTabs selTab;
        public XFItemStyle Style { get; set; }
        public XFItemType ItemType { get; set; }

        public XFItemProfileTabs()
        {
            curTab = ProfileTabs.Info;
            selTab = ProfileTabs.None;
            Style = new XFItemStyle()
            {
                Padding = 5
            };

            ItemType = XFItemType.Clickable;
        }

        #region IXFItem Members

        public XFPanelBase Parent { get; set; }

        public XFItemState State { get; set; }

        public int GetHeight()
        {
            return _tabHeigt + Style.PaddingTop + Style.PaddingBottom;
        }

        public void ResetHeight()
        {
        }

        public void ItemPaint(System.Drawing.Graphics g, int x, int y)
        {
            int width = Screen.PrimaryScreen.WorkingArea.Width;
            int tabWidth = (width / 3);


            using (SolidBrush b = new SolidBrush(Color.RoyalBlue))
            {
                g.FillRectangle(b, x, y, width, GetHeight());
            }

            if (curTab == ProfileTabs.Wall)
            {
                using (SolidBrush b = new SolidBrush(Color.White))
                    g.FillRectangle(b, x + Style.PaddingLeft, y + Style.PaddingTop + Style.PaddingBottom, tabWidth - (Style.PaddingLeft + Style.PaddingRight), _tabHeigt);

                using (SolidBrush b = new SolidBrush(Color.Black))
                    g.DrawString("Wall", new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), b, x + (_textPadding * 2), y + _textPadding);

                using (SolidBrush b = new SolidBrush(Color.SteelBlue))
                {
                    g.FillRectangle(b, x + Style.PaddingLeft + tabWidth, y + (Style.PaddingTop * 3), tabWidth - (Style.PaddingLeft * 2), _tabHeigt - (Style.PaddingTop * 2));
                    g.FillRectangle(b, x + Style.PaddingLeft + (tabWidth * 2), y + (Style.PaddingTop * 3), tabWidth - (Style.PaddingLeft * 2), _tabHeigt - (Style.PaddingTop * 2));
                }

                using (SolidBrush b = new SolidBrush(Color.White))
                {
                    g.DrawString("Info", new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), b, x + (_textPadding * 2) + tabWidth, y + _textPadding);
                    g.DrawString("Photos", new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), b, x + (_textPadding * 2) + (tabWidth * 2), y + _textPadding);
                }
            }
            else if (curTab == ProfileTabs.Info)
            {
                using (SolidBrush b = new SolidBrush(Color.White))
                    g.FillRectangle(b, x + Style.PaddingLeft + tabWidth, y + (Style.PaddingTop * 2), tabWidth - (Style.PaddingLeft * 2), _tabHeigt);

                using (SolidBrush b = new SolidBrush(Color.Black))
                    g.DrawString("Info", new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), b, x + (_textPadding * 2) + tabWidth, y + _textPadding);

                using (SolidBrush b = new SolidBrush(Color.SteelBlue))
                {
                    g.FillRectangle(b, x + Style.PaddingLeft, y + (Style.PaddingTop * 3), tabWidth - (Style.PaddingLeft * 2), _tabHeigt - (Style.PaddingTop * 2));
                    g.FillRectangle(b, x + Style.PaddingLeft + (tabWidth * 2), y + (Style.PaddingLeft * 3), tabWidth - (Style.PaddingLeft * 2), _tabHeigt - (Style.PaddingTop * 2));
                }

                using (SolidBrush b = new SolidBrush(Color.White))
                {
                    g.DrawString("Wall", new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), b, x + (_textPadding * 2), y + _textPadding);
                    g.DrawString("Photos", new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), b, x + (_textPadding * 2) + (tabWidth * 2), y + _textPadding);
                }
            }
            else
            {
                using (SolidBrush b = new SolidBrush(Color.White))
                    g.FillRectangle(b, x + Style.PaddingLeft + (tabWidth * 2), y + (Style.PaddingTop * 2), tabWidth - (Style.PaddingLeft * 2), _tabHeigt);

                using (SolidBrush b = new SolidBrush(Color.Black))
                    g.DrawString("Photos", new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), b, x + (_textPadding * 2) + (tabWidth * 2), y + _textPadding);

                using (SolidBrush b = new SolidBrush(Color.SteelBlue))
                {
                    g.FillRectangle(b, x + Style.PaddingLeft + tabWidth, y + (Style.PaddingTop * 3), tabWidth - (Style.PaddingLeft * 2), _tabHeigt - (Style.PaddingTop * 2));
                    g.FillRectangle(b, x + Style.PaddingLeft, y + (Style.PaddingTop * 3), tabWidth - (Style.PaddingLeft * 2), _tabHeigt - (Style.PaddingTop * 2));
                }

                using (SolidBrush b = new SolidBrush(Color.White))
                {
                    g.DrawString("Info", new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), b, x + (_textPadding * 2) + tabWidth, y + _textPadding);
                    g.DrawString("Wall", new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), b, x + (_textPadding * 2), y + _textPadding);
                }
            }
        }

        public XFItemClickResult GetClickResult()
        {
            if (selTab == ProfileTabs.None || selTab == curTab || User == null || Parent == null)
            {
                selTab = ProfileTabs.None;
                return XFItemClickResult.Nothing;
            }

            curTab = selTab;
            selTab = ProfileTabs.None;

            if (Buffer != null)
                Buffer.Dispose();
            Buffer = null;

            var par = Parent as XFPanelList;
            int pos = par[this] + 1;
            par.ClearItems(pos);

            switch (curTab)
            {
                case ProfileTabs.Info:
                    XFItemProfileInfo itm = new XFItemProfileInfo();
                    itm.User = User;
                    par.Add(itm);
                    break;
                case ProfileTabs.Wall:
                    XFWorkerLoadWall wall = new XFWorkerLoadWall(User);
                    XFItemLoading loading = new XFItemLoading(wall);

                    loading.WorkFinished += (o, e) =>
                        {
                            var usr = ((XFWorkerLoadWall)e.Worker).ParsedPersion;

                            if (!par.IsDisposed)
                                par.Invoke(new Action(() =>
                                    {
                                        par.ClearItemsAfterWaiting(loading);
                                        if (usr.Feed == null || usr.Feed.data == null || usr.Feed.data.Count == 0)
                                            par.Add("No Posts");
                                        else
                                            foreach (var f in usr.Feed.data)
                                            {
                                                XFItemPost post = new XFItemPost(f);
                                                par.Add(post);
                                            }
                                    }));
                        };

                    par.Add(loading);
                    FBFeed homeCache = new FBFeed();
                    homeCache.ParseCached(User.id);

                    foreach (var p in homeCache.data)
                    {
                        XFItemPost post = new XFItemPost(p);
                        par.Add(post);
                    }


                    break;
                case ProfileTabs.Photos:
                    par.Add("Coming Soon");
                    break;
            }

            ResetHeight();
            return XFItemClickResult.Nothing;
        }

        #endregion

        #region IXFItemReceiveClick Members

        public void GetClick(int x, int y)
        {
            int width = Screen.PrimaryScreen.WorkingArea.Width;
            int tabWidth = (width / 3);
            if (x < tabWidth)
                selTab = ProfileTabs.Wall;
            else if (x < (tabWidth * 2))
                selTab = ProfileTabs.Info;
            else
                selTab = ProfileTabs.Photos;
        }

        #endregion

        #region IXFItem Members


        public Bitmap Buffer { get; set; }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (Buffer != null)
                Buffer.Dispose();

            Buffer = null;
            if (Style != null)
                Style.Dispose();

            Style = null;
        }

        #endregion
    }
}
